package edu.zime.iot.controller;

import edu.zime.iot.entity.Course;
import edu.zime.iot.service.CourseService;
import edu.zime.iot.util.GeneralResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

public class UserController
{
    @Autowired
    private CourseService courseService;

    @GetMapping("/courses")
    public GeneralResponse courses()
    {
        GeneralResponse response=new GeneralResponse();
        response.setSuccess(true);
        response.setData(courseService.getAllCourses());
        return response;
    }
    @PostMapping("/course/add")
    public GeneralResponse add(@RequestParam String lesson, @RequestParam String teacher)
    {
        Course course=new Course();
        course.setLesson(lesson);
        course.setTeacher(teacher);

        GeneralResponse response=new GeneralResponse();
        response.setData(true);
        response.setData(courseService.add(course));
        return response;
    }
    @PutMapping("/course/update")
    public GeneralResponse update(@RequestParam String lesson,@RequestParam String teacher,@RequestParam int id)
    {
        Course course=new Course();
        course.setId(id);
        course.setLesson(lesson);
        course.setTeacher(teacher);

        GeneralResponse response=new GeneralResponse();
        response.setSuccess(true);
        response.setData(courseService.update(course));
        return response;
    }
    @GetMapping("/user")
    public GeneralResponse userById(@RequestParam int id)
    {
        GeneralResponse response=new GeneralResponse();
        response.setSuccess(true);
        List<Course> result=new ArrayList<>();
        result.add(courseService.getById(id));
        response .setData(result);
        return response;
    }
}
